package com.libl.controller;

import com.libl.pojo.DtsStorage;
import com.libl.service.DtsStorageService;
import com.libl.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/admin/storage")
public class DtsStorageController {
    @Autowired
    DtsStorageService storageService;

    //上传图片
    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception {
        byte[] buf =file.getBytes();

        DtsStorage storage = new DtsStorage();
        //name：原始文件名
        storage.setName(file.getOriginalFilename());
        //type：文件类型
        storage.setType(file.getContentType());
        //size：文件大小
        storage.setSize((int)file.getSize());

        storage= storageService.save(storage, buf);

        //响应前端
        return ResponseUtil.ok(storage);
    }

    //图片列表
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit",required = false, defaultValue = "20") Integer limit,
            String name , String key, String sort, String order) {
        /**
         * items
         * total
         */
        Map<String,Object> data = storageService.queryPage(page, limit,name,key,sort,order);
        return ResponseUtil.ok(data);
    }

}
